﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Library.DAL
{
    public class HoaDonXuatDAL
    {
        public static void HoanTatHoaDon(BIZ.HoaDonXuatBIZ hoadon)
        {
            NETMobileEntities db = new NETMobileEntities();
            var r = (from a in db.HoaDonXuats
                    where a.ID == hoadon.ID
                    select a).First();

            r.TrangThai = true;
            db.SaveChanges();
        }

        public static void Delete(BIZ.HoaDonXuatBIZ hoadon)
        {
            NETMobileEntities db = new NETMobileEntities();
            var r = (from a in db.HoaDonXuats
                     where a.ID == hoadon.ID
                     select a).First();
            db.HoaDonXuats.DeleteObject(r);
            db.SaveChanges();

        }
        public static int Add(BIZ.HoaDonXuatBIZ hoadon)
        {
            NETMobileEntities db = new NETMobileEntities();
            DAL.HoaDonXuat h = new HoaDonXuat();
            h.Ngay = hoadon.Ngay;
            h.GiaTri = hoadon.GiaTri;
            h.TenKH = hoadon.TenKH;
            h.DiaChiKH = hoadon.DiaChiKH;
            h.SoDienThoaiKH = hoadon.SoDienThoaiKH;
            h.TrangThai = false;
            db.HoaDonXuats.AddObject(h);
            db.SaveChanges();
            return h.ID;
        }

        public static List<BIZ.HoaDonXuatBIZ> FindAll(int trangthai)
        {
            // nếu f = 0 tìm các hóa đơn chưa xử lý, f = 1 tìm các hóa đơn đã xử lý, f = -1 tìm hết
            List<BIZ.HoaDonXuatBIZ> listhoadon = new List<BIZ.HoaDonXuatBIZ>();
            NETMobileEntities db = new NETMobileEntities();

            if (trangthai == -1)
            {
                var r = from a in db.HoaDonXuats select a;

                foreach (DAL.HoaDonXuat h in r)
                {
                    BIZ.HoaDonXuatBIZ hoadon = new BIZ.HoaDonXuatBIZ();
                    hoadon.ID = h.ID;
                    hoadon.Ngay = h.Ngay;
                    hoadon.GiaTri = h.GiaTri;
                    hoadon.TenKH = h.TenKH;
                    hoadon.DiaChiKH = h.DiaChiKH;
                    hoadon.SoDienThoaiKH = h.SoDienThoaiKH;
                    hoadon.TrangThai = h.TrangThai;
                    listhoadon.Add(hoadon);
                }
            }
            else
            {
                bool t = (trangthai == 0) ? false : true ;
                var r = from a in db.HoaDonXuats 
                        where a.TrangThai == t
                        select a;

                foreach (DAL.HoaDonXuat h in r)
                {
                    BIZ.HoaDonXuatBIZ hoadon = new BIZ.HoaDonXuatBIZ();
                    hoadon.ID = h.ID;
                    hoadon.Ngay = h.Ngay;
                    hoadon.GiaTri = h.GiaTri;
                    hoadon.TenKH = h.TenKH;
                    hoadon.DiaChiKH = h.DiaChiKH;
                    hoadon.SoDienThoaiKH = h.SoDienThoaiKH;
                    hoadon.TrangThai = h.TrangThai;
                    listhoadon.Add(hoadon);
                }
            }

            return listhoadon;

        }
    }
}
